import {ansBase, ansData} from "../base/common/Constants";
import {backABCD, backByCount} from "../base/utils/Utils";
import Toast from "../base/utils/Toast";
import global from "../base/common/Global";
import {Page} from "../base/nav/Router";
import store from "../Store";
import {NetWork} from "../base/net/NetWork";

const {ccclass, property} = cc._decorator;

@ccclass
export default class AnsLogic extends cc.Component {


    @property(cc.Label) countLabel: cc.Label = null;
    @property(cc.Label) titleLabel: cc.Label = null;
    @property(cc.Node) ansNode: cc.Node = null;

    @property(cc.Node) ansPanel: cc.Node = null;
    @property(cc.Node) winPanel: cc.Node = null;
    @property(cc.Node) losePanel: cc.Node = null;


    @property(cc.Label) topLabel: cc.Label = null;
    // @property(cc.SpriteFrame) topSprImgs: cc.SpriteFrame[] = [];


    curCount: number = 0;
    allCount: number = 0;
    allAnsData: ansBase[] = [];

    curAns: number = 0;

    canClick: boolean = false;
    win: boolean = true;

    curAnsCount: number = 0; // 当前问题选项数量

    canControl:boolean = false;

    init() {
        this.ansPanel.active = true;
        this.winPanel.active = false;
        this.losePanel.active = false;
        this.canClick = false;
        this.win = true;
        this.curCount = 0;
        this.allCount = 3;
        this.allAnsData = [];
        this.canControl = false;

        // let backByCount1 = backByCount(ansData, 3);
        // if (backByCount1 != undefined) {
        //     for (let i = 0; i < backByCount1.length; i++) {
        //         this.allAnsData.push(ansData[backByCount1[i]])
        //     }
        // }
        this.getAns();

        this.topLabel.string = store.gameWin ? '已经逃出火场，\n恭喜你活了下来' : '你被火焰吞噬了，\n就差那么一点';
    }

    getAns() {
        let cou = {
            count: 3
        }
        NetWork.post1('http://221.239.87.234:10521/Subjects', JSON.stringify(cou), (res) => {
            if (res) {
                if (res.success == 'true') {
                    for (let i = 0; i < res.data.length; i++) {
                        let a = [];
                        for (let j = 0; j < res.data[i].options.length; j++) {
                            a.push(res.data[i].options[j].option);
                        }
                        this.allAnsData.push({
                            title: res.data[i].subject,
                            ans: a,
                            sure: res.data[i].answer,
                            type: res.data[i].options.length > 2 ? 1 : 0
                        })

                    }
                    this.showAns();

                } else {
                    Toast(res.error)
                }
            } else {
                Toast('网络连接异常');
            }

        })
    }


    showAns() {
        this.curAns = 0;

        this.canClick = true;

        this.countLabel.string = `(${this.curCount + 1}/${this.allCount})`;
        this.titleLabel.string = `${this.allAnsData[this.curCount].title}`;
        for (let i = 0; i < this.ansNode.childrenCount; i++) {
            if (this.allAnsData[this.curCount].ans.length > i) {
                this.ansNode.children[i].active = true;
                this.ansNode.children[i].children[0].children[0].children[0].children[0].active = false;
                this.ansNode.children[i].children[0].children[0].children[1].active = false;
                this.ansNode.children[i].children[0].children[0].children[2].active = false;
                this.ansNode.children[i].children[0].getComponent(cc.Label).string = `${this.allAnsData[this.curCount].ans[i]}`;
            } else {
                this.ansNode.children[i].active = false;
            }
        }
        this.curAnsCount = this.allAnsData[this.curCount].ans.length;

        this.showAnsNodeState();
        this.scheduleOnce(()=>{
            this.canControl = true;
            Toast('可以答题了！');
        },1)

    }

    upAns() {
        if (this.curAns > 0) {
            this.curAns--;
            this.showAnsNodeState()
        }
    }

    downAns() {
        if (this.curAns < (this.curAnsCount - 1)) {
            this.curAns++;
            this.showAnsNodeState()
        }
    }

    setAns(c){
        if(this.allAnsData[this.curCount].ans.length > 2){
            this.curAns = c;

        }else {
            if(c < 2){
                this.curAns = c;
            }
        }
        this.showAnsNodeState()

    }

    onClickAns(event, data) {
        this.curAns = backABCD(data);
        this.showAnsNodeState()

    }

    showAnsNodeState() {
        for (let i = 0; i < this.ansNode.childrenCount; i++) {
            this.ansNode.children[i].children[0].children[0].children[0].children[0].active = this.curAns == i;
        }
    }

    onClickSure() {

        if (!this.canClick || !this.canControl) {
            return;
        }
        this.canClick = false;
        this.canControl = false;
        let res :boolean = false;
        console.log(this.allAnsData)
        console.log(this.curCount)
        // 判断题
        if(this.allAnsData[this.curCount].type == 0){
            res = this.allAnsData[this.curCount].ans[this.curAns] == this.allAnsData[this.curCount].sure;
        }else {
            res=  this.allAnsData[this.curCount].ans[this.curAns][0] ==  this.allAnsData[this.curCount].sure;
        }

        if (res) {
            // 答对了！
            this.ansNode.children[this.curAns].children[0].children[0].children[1].active = true;

        } else {
            // 答错了！

            this.ansNode.children[this.curAns].children[0].children[0].children[2].active = true;
            let c = 0;
            for(let i = 0; i < this.allAnsData[this.curCount].ans.length; i++){
                if(this.allAnsData[this.curCount].ans[i][0] == this.allAnsData[this.curCount].sure){
                    c=i;
                    break;
                }
            }

            this.ansNode.children[c].children[0].children[0].children[1].active = true;
            this.win = false;
        }


        cc.tween(this.node)
            .delay(1)
            .call(() => {
                this.curCount++;
                if (this.curCount >= this.allCount) {
                    if (this.win) {
                        this.winPanel.active = true;
                    } else {
                        this.losePanel.active = true;
                    }
                } else {
                    this.showAns();
                }
            })
            .start()


    }

    onClickGame() {
        if (store.gameWin) {
            global.router.navigate(Page.Game);

            global.loginLogic.gameLogic.curLevel++;

            global.loginLogic.gameLogic.init();

        } else {
            global.router.navigate(Page.Game);
            global.loginLogic.gameLogic.init();
        }

    }

    onClickRest() {
        this.init();
    }


}
